package com.validated.constraint;

import com.validated.annotation.Digits;
import com.validated.util.ObjectUtil;
import java.math.BigDecimal;

/**
 * 数字约束
 *
 * @author Moo
 * @date 2023/05/11
 */
public class DigitsConstraint implements ConstraintValidator<Digits, Object> {

	@Override
	public boolean isValid(Digits annotation, Object objectValue, Class<?> valueType) {
		if(annotation.enableNull() && null == objectValue){
			return true;
		}
		if (objectValue instanceof Number){
			return true;
		}else if (objectValue instanceof BigDecimal ) {
			return true;
		}else{
			String value = objectValue == null ? "" : objectValue.toString();
			return ObjectUtil.isDigits(value);
		}
	}

}
